package com.qin.EducationalManger.teacher.dao;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.qin.EducationalManger.student.model.StudentModel;
import com.qin.EducationalManger.teacher.model.TeacherModel;
import com.qin.EducationalManger.user.model.UserModel;
import com.qin.EducationalManger.utils.JdbcUtils;


public class TeacherDao {
	JdbcUtils jdbcUtils = new JdbcUtils();

	public static int insert(TeacherModel model) {

		String sql = "insert into teacher (teacher_id,teacher_name,teacher_sex,title,course_id,tel) values(?,?,?,?,?,?)";
		List<Object> vals = Arrays.asList(model.getTeacherId(), model.getTeacherName(), model.getTeacherSex(),
				model.getTitle(), model.getCourseId(),model.getTel());

		return JdbcUtils.update(sql, vals);

	}

	public static int delet(TeacherModel mode) {
		String sql = "delete from teacher where teacher_id like ?";
		List<Object> vals = Arrays.asList(mode.getTeacherId());

		
		
		return JdbcUtils.update(sql, vals);

	}
	
	//按照条件修改

	public static int update(TeacherModel mode) {

		StringBuffer sql = new StringBuffer("update teacher set teacher_id ="+mode.getTeacherId()); // 储存sql语句
		List<Object> vals = new ArrayList<Object>(); // 用于存储 要修改的 内容
       
		String teacherName = mode.getTeacherName(); // 判断是否要对该值做出修改
		if (teacherName != null && teacherName.intern().isEmpty() == false) {
			sql.append(" ,teacher_name = ?");
			vals.add(teacherName);
		}

		
		String teacherSex = mode.getTeacherSex();
		if (teacherSex != null && teacherSex.intern().isEmpty() == false) {
			sql.append(",teacher_sex = ?");
			vals.add(teacherSex);
		}


		String courseId = mode.getCourseId();
		if(courseId!=null&&courseId.intern().isEmpty()==false) {
			sql.append(",course_id = ?");
			vals.add(courseId);
		}
		String title = mode.getTitle();
		if(title!=null&&title.intern().isEmpty()==false) {
			sql.append(",title = ?");
			vals.add(title);
		}
		String tel = mode.getTel();
		if(tel!=null&&tel.intern().isEmpty()==false) {
			sql.append(",tel = ?");
			vals.add(tel);
		}
		
		
		sql.append(" where teacher_id = " + mode.getTeacherId());

		return JdbcUtils.update(sql.toString(), vals);

	}
	
	//按照条件查询
	public  static List<TeacherModel> select(TeacherModel model) {
	StringBuffer sql = new StringBuffer("select * from teacher where 1=1 ");
	
	Map<String, String> fields = new HashMap<String, String>();
	fields.put("teacherId", "teacher_id");
	fields.put("teacherName", "teacher_name");
	fields.put("title", "title");
	fields.put("teacherSex", "teacher_sex");
	fields.put("courseId", "course_id");
	fields.put("tel", "tel");
    
	
	
	
	List<Object> vals = appendWhere(sql, model);
	
	
	return  JdbcUtils.Query(sql.toString(), vals, TeacherModel.class, fields);
	
	}
	
	
	private static List<Object>  appendWhere(StringBuffer sql, TeacherModel model) {
		
		List<Object> vals = new ArrayList<Object>();
		String teacherName = model.getTeacherName();
		if(teacherName!=null&&teacherName.intern().isEmpty()==false) {
			sql.append(" and teacher_name like ?");
			vals.add(teacherName);
		}
		String teacherId = model.getTeacherId();
		if(teacherId!=null&&teacherId.intern().isEmpty()==false) {
			sql.append(" and teacher_id like ?");
			vals.add(teacherId);
		}
	
		String title = model.getTitle();
		if(title!=null&&title.intern().isEmpty()==false) {
			sql.append(" and title=?");
			vals.add(title);	
		}
		String teacherSex = model.getTeacherSex();
		if(teacherSex!=null &&teacherSex.intern().isEmpty()==false) {
			sql.append(" and teacher_sex=?");
			vals.add(teacherSex);
		}
		String tel = model.getTel();
		if(tel!=null &&tel.intern().isEmpty()==false) {
			sql.append(" and tel=?");
			vals.add(tel);
		}
				
		String courseId = model.getCourseId();
		if(courseId!=null&&courseId.intern().isEmpty()==false) {
			
			sql.append(" and course_id =?");
			vals.add(courseId);
		}
		
		if(model.isPageOn()) {
			sql.append("limit ?,?");
			vals.add(model.getRowStart());
			vals.add(model.getPageLimit());
			
		}
		
		
		return vals;
		
		
	}
	// 查询信息总条数
	public static Integer selectCount(TeacherModel model) {
		String teacherId = model.getTeacherId();
		String teacherName = model.getTeacherName();
		List <Object> vals = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer("select count(teacher_id) as count from teacher where 1=1");
		if(teacherId!=null&&teacherId.intern().isEmpty()==false) {
			sql.append(" and teacher_id like ?");
			vals.add(teacherId);
		}
		
		
		if(teacherName!=null&&teacherName.intern().isEmpty()==false) {
			sql.append(" and teacher_name like ?");
			vals.add(teacherName);
		}
	
	
		return JdbcUtils.selectCount(sql.toString(),vals);
		
	}

}
